<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { showToast } from 'vant';
import YHeader from '@/components/YHeader.vue';
import type { Course } from '@/api/classroom/type';
import { useUserStore } from '@/store/user';
import { v4 as uuidv4 } from 'uuid';
import ClassBody from './components/ClassBody.vue';
import { getCourseDetail } from '@/api/classroom';
// 模拟数据，实际应该从API获取
let courseInfo = ref<Course>({
  name: '',
  code: '',
  term: '',
  description: '',
  requirements: '',
  coverImage: '',
  joinCode: '',
  maxStudent: 0,
  teacherId: ''
});

const route = useRoute();
const router = useRouter();
const isTeacher = ref(false);
const courseId = ref<string>('');

const checkRole = () => {
  const userStore = useUserStore();
  let userRoleCode = userStore.getRoleCode;
  if (userRoleCode == 'TEACHER') {
      isTeacher.value = true;
  }else{
      isTeacher.value = false;
  }
}

onMounted(async () => {
  checkRole();
  const courseId = route.query.courseId  
  if (!courseId) {
    showToast('没有传递课程ID');
    return;
  }
  try {
    const res = await getCourseDetail(courseId);
    if (res.code === 200) {
      courseInfo.value = res.data;
    } else {
      showToast(res.data.msg || '获取课程详情失败');
    }
  } catch (error) {
    console.error('请求课程详情失败:', error);
    showToast('请求课程详情失败');
  }
});




const navigateTo = (path: string) => {
  router.push({
    path,
    query: { courseId: courseInfo.value.id }
  });
};

const copyCode = () => {
  if (courseInfo.value.code) {
    navigator.clipboard.writeText(courseInfo.value.joinCode)
      .then(() => {
        showToast('邀请码已复制');
      })
      .catch(err => {
        console.error('复制失败', err);
        showToast('复制失败');
      });
  }
};
</script>
<template>
    
    <div class="classroom-manage">
      <!-- <y-header title="课堂管理" left-arrow @click-left="onBack" /> -->
      <YHeader></YHeader>
      <div class="course-info">
        <div class="course-avatar">
          <van-image
            round
            width="60"
            height="60"
            :src="courseInfo.coverImage || 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'"
          />
        </div>
        <div class="course-detail">
            <h3>{{ courseInfo.name || '未命名课程' }}</h3>
            <p>课程简介：{{ courseInfo.description || '未知' }}</p>

            <div class="course-code-section" >
        <div class="code-title">邀请码:
          <span  class="code,code-content" @click="copyCode" title="点击复制邀请码">
              {{ courseInfo.joinCode }}
            </span>
        </div>
      </div>
      </div>
      </div>
        

      <ClassBody :courseId="route.query.courseId"></ClassBody>
      <van-grid :column-num="2" :gutter="10" square v-if="isTeacher">
        <van-grid-item 
          v-for="(item, index) in managementOptions" 
          :key="index"
          :icon="item.icon"
          :text="item.text"
          @click="navigateTo(item.path)"
        />
      </van-grid>
      
      
    </div>
  </template>
  

  
  <style scoped lang="scss">
.classroom-manage {
  background-color: #f7f8fa;
  min-height: 100vh;
  padding-bottom: 20px;
}

.course-info {
  margin: 16px;
  padding: 16px;
  background-color: #fff;
  border-radius: 8px;
  display: flex;
  align-items: flex-start; /* 头像与文本顶部对齐 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.course-avatar {
  margin-right: 16px;
}

.course-detail {
  h3 {
    margin: 0 0 8px 0;
    font-size: 18px;
    font-weight: 600;
  }
  p {
    margin: 4px 0;
    color: #646566;
    font-size: 14px;
  }
  /* 邀请码区域 */
  .course-code-section {
    margin-top: 8px;
    padding: 8px;
    background-color: #fafafa;
    border-radius: 4px;
    
    .code-title {
      font-size: 14px;
      font-weight: 500;
      margin-bottom: 4px;
    }
    .code-content {
      display: flex;
      align-items: center;
      
      .code {
        font-size: 16px;
        font-weight: 500;
        color: #1989fa;
        margin-right: 8px;
        cursor: pointer;
        transition: color 0.3s;
      }
      .code:hover {
        color: #0d74d1;
      }
    }
    .code-tip {
      font-size: 13px;
      color: #969799;
      margin-top: 4px;
    }
  }
}
  </style>